//
//  XYTabBar.m
//  OC_weibo
//
//  Created by 李旭阳 on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "XYTabBar.h"

@interface XYTabBar()

@property (nonatomic, weak) UIButton *plusButton;

@end

@implementation XYTabBar

//编译器认为我们自己定义的成员与父类重名,会有我们自己的逻辑,我们如果没有自己的逻辑的话,可以使用@dynamic标识
@dynamic delegate;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [[UIButton alloc] init];
        
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //button 的大小 等于当前北京图片的大小
        button.size = button.currentBackgroundImage.size;
        
        //添加点击事件
        [button addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        self.plusButton = button;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.plusButton.centerX = self.width * 0.5;
    self.plusButton.centerY = self.height * 0.5;
    
    NSInteger count = self.subviews.count;
    
    NSInteger index = 0;
    
    CGFloat buttonWidth = self.width / 5;
    
    for (int i = 0; i < count; ++i) {
        
        UIView *childView = self.subviews[i];
        
        if ([childView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            childView.width = buttonWidth;
            childView.x = buttonWidth * index;
            
            index++;
            
            if (index == 2) {
                index++;
            }
        }
    }
}

-(void)plusButtonClick:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectPlusButton:)]) {
        [self.delegate tabBar:self didSelectPlusButton:button];
    }
}

@end
